In relation with extensions of predefined classes in ooRexx.
The title "Expression problem" is not good for ooRexx because it's about static
type safety (e.g., no casts)
https://en.wikipedia.org/wiki/Expression_problem
Adding "Extension" in the title.


https://homepages.inf.ed.ac.uk/wadler/papers/expression/expression.txt
The Expression Problem
Philip Wadler, 12 November 1998
---
jlf: "original formulation"
The Expression Problem is a new name for an old problem.  The goal is
to define a datatype by cases, where one can add new cases to the
datatype and new functions over the datatype, without recompiling
existing code, and while retaining static type safety (e.g., no
casts).


https://gavinhoward.com/2025/04/how-i-solved-the-expression-problem/
How I Solved the Expression Problem
28 Apr 2025
According to the original formulation, there are only four prongs:
“add new cases to [a] datatype”
“add new functions over [a] datatype”
“without recompiling existing code”
“while retaining static type safety (e.g., no casts)”
---
jlf: hum, this author created the Yao language, but impossible to consult the
sources. Lot of blah-blah in his blog, even if some links are interesting.


--------------------------------------------------------------------------------
C++
--------------------------------------------------------------------------------

https://github.com/jll63/yomm2
Fast, orthogonal, open multi-methods.
Solve the Expression Problem in C++17.


--------------------------------------------------------------------------------
C#
--------------------------------------------------------------------------------

http://msdn.microsoft.com/en-us/library/bb383977.aspx
Extension Methods (C# Programming Guide)


--------------------------------------------------------------------------------
Clojure
--------------------------------------------------------------------------------

https://clojure.org/reference/protocols
http://adambard.com/blog/structured-clojure-protocols-and-multimethods/
Structured clojure: Protocols and multimethods
http://clojure.org/multimethods
http://stackoverflow.com/questions/4509782/simple-explanation-of-clojure-protocols

http://www.ibm.com/developerworks/java/library/j-clojure-protocols/index.html
http://www.reddit.com/r/programming/comments/fp0re/solving_the_expression_problem_with_clojure/


--------------------------------------------------------------------------------
Haskell
--------------------------------------------------------------------------------

https://eli.thegreenplace.net/2018/more-thoughts-on-the-expression-problem-in-haskell/
More thoughts on the Expression Problem in Haskell
February 05, 2018
Tags Haskell , Multiple dispatch


--------------------------------------------------------------------------------
Java
--------------------------------------------------------------------------------

http://www.wiki.jvmlangsummit.com/images/7/71/2011_Goetz_Extension.pdf
Virtual extension methods


--------------------------------------------------------------------------------
Javascript
--------------------------------------------------------------------------------

http://perfectionkills.com/extending-native-builtins/

http://javascriptweblog.wordpress.com/2011/12/05/extending-javascript-natives/


--------------------------------------------------------------------------------
Kotlin
--------------------------------------------------------------------------------

https://www.reddit.com/r/Kotlin/comments/de6o4e/kotlin_extension_functions_vs_ruby_and_java/
kotlin extension functions vs ruby and java script monkey patching?
It is not the same.
First, for you to use an extension function, you need to import it.
Second, they don't show up if you use reflection to look at the class.
Third, since you are actually not modifying the class, you can have 2 extension
methods with the same name and signature in the same program, and not have any
error (let's say they are private, and are in libraries you are using).
The way monkey patch works is like you said, by adding a new method to a class.
The way extension functions work, is by creating a regular function, and just
giving you a nice syntactic sugar for accessing it.


--------------------------------------------------------------------------------
Magpie
--------------------------------------------------------------------------------

http://journal.stuffwithstuff.com/2011/04/21/multimethods-multiple-inheritance-multiawesome/
http://journal.stuffwithstuff.com/2010/10/01/solving-the-expression-problem/


--------------------------------------------------------------------------------
Python Monkey-patching
--------------------------------------------------------------------------------

https://www.pythonforthelab.com/blog/monkey-patching-and-its-consequences/
Python

https://machinelearningmastery.com/monkey-patching-python-code/
Python


--------------------------------------------------------------------------------
Ruby Monkey-patching
--------------------------------------------------------------------------------

https://blog.appsignal.com/2021/08/24/responsible-monkeypatching-in-ruby.html
Responsible Monkeypatching in Ruby
jlf: to read (ruby)

https://shopify.engineering/the-case-against-monkey-patching
The Case Against Monkey Patching, From a Rails Core Team Member
jlf: to read (ruby)

https://shopify.engineering/the-case-against-monkey-patching
The Case Against Monkey Patching, From a Rails Core Team Member

https://www.justinweiss.com/articles/3-ways-to-monkey-patch-without-making-a-mess/
Ruby

https://blog.daveallie.com/clean-monkey-patching
Ruby Clean Monkey Patching

https://stackoverflow.com/questions/30757126/why-the-module-classmethods-defined-and-extended-in-the-same-namespace
Ruby
This is actually a pretty common practice in Ruby.
Basically, what it's saying is: when some object performs include MyModule, make
it also extend MyModule::ClassMethods. Such a feat is useful if you want a mixin
that adds some methods not just to the instances of a class, but to the class itself.

http://blog.headius.com/2012/11/refining-ruby.html
Ruby

http://yehudakatz.com/2010/11/30/ruby-2-0-refinements-in-practice/
Ruby
The purpose of refinements in Ruby 2.0 is to make monkey-patching safer.
Specifically, the goal is to make it possible to extend core classes, but to
limit the effect of those extensions to a particular area of code.

https://alchemists.io/articles/ruby_refinements
Ruby Refinements
jlf: good explanation of lexical scopes in Ruby.
Lexical scope is defined at the point of origin/definition, not where the code is used.
Refinements are activated within current and nested scopes only.

https://bugs.ruby-lang.org/issues/11779
Module#using does not make sense as a method
jlf: maybe interesting to read (examples, clarifications)

https://bugs.ruby-lang.org/issues/18572
Performance regression when invoking refined methods
jlf: interesting, discussion about cache, several examples of refinement

https://www.reddit.com/r/ruby/comments/1553yca/has_anybody_actually_been_able_to_use_rubys/
Has anybody actually been able to use Ruby's Refinements in the real world?
jlf: mixed opinions
f9ae8221b
    "IMHO refinements suck up a ton of development work for an extremely meager usage."
Inevitable-Swan-714
    "I'd argue that they only have meager usage because they largely miss the mark on making monkey patching safer."
f9ae8221b
    "Refinements are based on the lexical scope, not the instance. So yeah if you return a refined typed, it doesn't impact the called."
f9ae8221b
    I fondly remember that require 'aws-sdk' would cause a bunch of silly monkey patch to be added to Array and Hash.
    That's why ruby-core looked for saner ways to extend types.
    But ultimately the dust settled and the original problem kinda solved itself "socially" rather than via a new feature.
    Gems authors wouldn't dare monkey patching core classes anymore and the few which do would immediately get a PR to remove it.

https://railsfactory.com/the-art-of-monkey-patching-ruby
jlf: very minimal


--------------------------------------------------------------------------------
Rust
--------------------------------------------------------------------------------

http://xion.io/post/code/rust-extension-traits.html


--------------------------------------------------------------------------------
Shen
--------------------------------------------------------------------------------

https://groups.google.com/forum/?hl=en&fromgroups#!topic/qilang/uGNHTS9LII4
The expression problem


--------------------------------------------------------------------------------
Swift
--------------------------------------------------------------------------------

https://docs.swift.org/swift-book/documentation/the-swift-programming-language/extensions/
Extensions add new functionality to an existing class, structure, enumeration, or protocol type.
This includes the ability to extend types for which you do not have access to the original source code (known as retroactive modeling).
Extensions can add new functionality to a type, but they cannot override existing functionality.
---
Note
Extensions can add new computed properties, but they can’t add stored properties,
or add property observers to existing properties.
jlf: ooRexx is more powerful because it's possible to add stored properties.
That is the killer feature! Any object, including an instance of predefined
class, and the class itself, has a pool of variables.

https://docs.swift.org/swift-book/documentation/the-swift-programming-language/protocols/#Protocol-Extensions
Swift Protocol Extensions


https://stackoverflow.com/questions/37104683/are-there-other-languages-that-have-something-like-swifts-extensions
Swift extensions are not monkey patching.


--------------------------------------------------------------------------------
Unsorted
--------------------------------------------------------------------------------


http://lambda-the-ultimate.org/node/4136


https://eli.thegreenplace.net/2016/the-expression-problem-and-its-solutions/
The Expression Problem and its solutions
May 12, 2016
Tags C & C++ , Clojure , Multiple dispatch , Haskell


--------------------------------------------------------------------------------
ooRexx
--------------------------------------------------------------------------------

Some discussions in February 2011 about a better way to support extensions.
At today 2024.02.01, nothing has been started to explore these ideas.


https://sourceforge.net/p/oorexx/mailman/message/27087090/
    > Just a few words about the changes I made in my sandbox : in fact I made
    > almost nothing ! Everything is in place in the current interpreter to
    > support this kind of extension... I had just to unlock the 'define' method
    > and the 'inherit' method. And do a little change to let impact the already
    > existing instances when adding a new method in the behavior.
    > The rest is only syntax sugar (::extension).
    >
    > Currently, what is missing is a control to forbid the overriding of a
    > predefined method (for the reasons explained in a previous mail).

    I'm definitely -1 to implementing this feature in this manor.  I think
    the concept of a class extender needs to be part of the model for how
    a class works, including the implementation of the restriction on
    method overrides.  It should be possible to query the extensions on
    what has been added to a class and find out the specific methods that
    have been added.  Then the ::extension directive should just map to
    those underlying class methods.

    However, I have one serious concern about the fragility of extensions.
     Since these are not permitted to override methods of the class they
    are being attached to, they become vulnerable to any changes to the
    set of methods the target class may add in the future.  This would
    mean either A) we must never add new methods to a class or B) have to
    be very explicit about the possibility of release-to-release breakage.

    Rick


https://sourceforge.net/p/oorexx/mailman/message/27087244/
    > 2011/2/19 Rick McGuire <obj...@gm...>
    >>
    >> I'm definitely -1 to implementing this feature in this manor.
    >
    > Rick, do you mean "in this manner" here ?  just to understand if the feature
    > could be accepted, but with a different approach, or if the feature is
    > really not wanted.

    I am -1 to implementing this by using inherit and define and removing
    the restrictions.  The concept of extensions should be a new
    capability of classes, not just bolted over the existing mechanisms.

    Rick


https://sourceforge.net/p/oorexx/mailman/message/27087368/
    > ok :-)
    > Needless to say that i'm *very* interested by more details about what could
    > be this new capability.


    Well, by new capability, I mean new methods that implement this
    mechanism.  As a general principle, the directive instructions just
    map to methods (or a series of methods) on the Class class.  It is
    possible to create a new class just by invoking the methods, though
    that is rarely done.  So adding an extension to a class should be done
    via an "extend" method.  There should probably be a method that will
    return any extensions that have been added to a class.  There are also
    some fundamental questions that need to be defined (i.e., write the
    docs first) before this can be implemented.  Such as,

    1) what sort of class can be added as an extension?  Do we introduce a
    new class type?  Or just override mixin class?
    2)  Can we extend via individual methods?  Right now, I favor just
    doing this via a class mechanism rather than by adding on methods
    because of scope and other concerns.  This is a restriction that can
    be easily lifted in a future release if there is sufficient demand,
    but there are enough areas of concern that I don't favor doing this
    initially.
    3)  Handling the fragile base-class problem.
    4)  What happens if a class is extended, then it inherits from a mixin
    to create a new conflict?

    Rick


https://sourceforge.net/p/oorexx/mailman/message/27087620/
    >> 1) what sort of class can be added as an extension?  Do we introduce a
    >> new class type?  Or just override mixin class?
    >
    > I thought that extending via individual methods was the right way (your
    > point 2). The extension as a class may forbid to distribute the extension
    > methods in several places. I find this capacity to distribute the extensions
    > of a given class in several files very useful. See examples here :
    > http://oorexx.svn.sourceforge.net/viewvc/oorexx/sandbox/jlf/samples/extension

    No, absolutely not.  Adding methods individually introduces a
    complication of what scope should be used by the method.  Methods
    added via the define method have a common scope, which means they
    share a common poll of variables.  It would be inappropriate for
    extension methods to have access to the same variables used by methods
    defined by the class itself or for methods from unrelated extensions
    to share variables.  By restricting this to just class-based
    extensions, the isolation and encapsulation is maintained.  I am
    definitely -1 to individual method extension.

    > Last point : my sandbox is currently in stable state. So anyone interested
    > in testing the ::extension can get it and build.
    > Then the file to play with is sample/extension/functional-test.rex

    This is exactly what I mean by "you need to write the definition
    first".  You first need to understand what the methods are for a
    class, and what the restrictions are on usage.  And this needs to be
    generalized and not just defined for the predefined classes, but all
    classes.  The predefined ones just have some additional restrictions.
    For example, what happens with a sequence like this:

    .aClass~inherit(.addsFoo)   -- adds a "foo" method
    .aClass~extend(.fooExtension)  -- also adds a "foo" method, which will
    give an error

    vs.

    .aClass~extend(.fooExtension)  -- adds a "foo" method without error
    because there is no conflict
    .aClass~inherit(.addsFoo)  -- There is now a conflict caused by the
    extension...how to handle that.

    Note that in the second case, the inherit() method ends up creating a
    conflict caused by an extension that the issuer of the inherit method
    does not know about.
